package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")

public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
        log.info("添加地址:{}",addressBook);
        addressBookService.save(addressBook);
        return Result.success("添加地址成功");
    }
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> addressBooks = addressBookService.list();
        return Result.success(addressBooks);
    }

    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress(){
        AddressBook addressBook = addressBookService.getDefaultAddress();
        return Result.success(addressBook);
    }
    @PutMapping("/default")
    public Result setDefaultAddress(@RequestBody AddressBook addressBook){
        log.info("修改菜品Id:{}",addressBook);
        addressBookService.setDeafultAddress(addressBook);
        return Result.success("修改默认地址成功");
    }

    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success("修改地址成功");
    }

    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @DeleteMapping
    public Result delete(Long id){
        addressBookService.delete(id);
        return Result.success("删除地址成功");
    }
}
